<?php
include 'animation.class.php';
$imPath = "D:\\ImageMagick";
$animation = new Animation($imPath);
$listFilePath = "pngfilelist.csv";
$srclist = $animation->readListFile($listFilePath);
$outputani = "imagickAni.gif";
$aniXsize = 512;
$aniYsize = 512;
$outXsize = 512;
$outYsize = 512;
$fps = 2;
$delay = round(100 / $fps);
$beTransparentedColor = "#000000";

$draw = new ImagickDraw();
$bg = "bgs/bg1.png";
$bglayer = new Imagick($bg);
$fg = "bgs/fg1.png";
$fglayer = new Imagick($fg);
try{
    $aniGif = new Imagick();

    foreach($srclist as $src){
        $filename = $src[0];
        $filedate = $src[1];
        echo "-----------------------\n adding frame {$filename}\n";
        $frame = new Imagick($filename);
        //transparency
        $frame->paintTransparentImage($beTransparentedColor, $alpha = 0.0, $fuzz = 0);
        //resize
        $frame->resizeImage($aniXsize, $aniYsize, Imagick::FILTER_POINT, $blurry = 1);
        //adding background
        $frame->compositeImage($bglayer, imagick::COMPOSITE_OVERLAY, $x = 0, $y = 0);
        //adding annotation
        $frame->annotateImage($draw, $x = 10, $y = 45, $angle = 0, "this is hello from word");
        //addding foreground
        $fglayer->compositeImage($frame, imagick::COMPOSITE_OVERLAY, $x = 0, $y = 0);

        $aniGif->addImage($fglayer);
        $aniGif->setImageDelay($delay);
        echo "end of adding frame {$filename}\n";
    }
    //adding background;
//    $bgfile = "bgs/bg1.png";
//    $background = new Imagick($bgfile);
//    $aniGif->addImage($bgfile);
//    //adding foreground;
//    $fgfile = "bgs/fg1.png";
//    $foreground = new Imagick($fgfile);

    $aniGif->writeImages($outputani, $adjoin = true);
    echo "end of writing";
}catch(ImagickExecption $imEx){
    echo "Error:".$imEx->getMessage();
}